﻿using System.Net;
using System.Net.Mail;

namespace ECommon
{
    /// <summary>
    /// 邮箱工具
    /// </summary>
    public static class EmailTool
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="fromMailAddress">发送人邮箱地址</param>
        /// <param name="toMailAddress">接收人邮箱地址</param>
        /// <param name="emailUserNo">发送人邮箱账号</param>
        /// <param name="emailPwd">发送人邮箱密码</param>
        /// <param name="emailServer">发送人邮箱服务器</param>
        /// <param name="emailPort">发送人邮箱服务端口</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="content">邮件内容</param>
        /// <param name="isSsl">是否采用安全链接发送</param>
        /// <param name="attachFilePath">邮件附件</param>
        /// <param name="cc">邮件抄送人</param>
        public static void SendEmail(string fromMailAddress, string toMailAddress, string emailUserNo, string emailPwd, string emailServer, int emailPort, string subject, string content, bool isSsl, string attachFilePath, string cc)
        {
            if (string.IsNullOrWhiteSpace(fromMailAddress) || string.IsNullOrWhiteSpace(emailUserNo) || string.IsNullOrWhiteSpace(emailPwd) || string.IsNullOrWhiteSpace(toMailAddress) || string.IsNullOrWhiteSpace(emailServer) || string.IsNullOrWhiteSpace(subject) || string.IsNullOrWhiteSpace(content))
            {
                throw new ArgumentNullException("fromMailAddress, toMailAddress, emailUserNo, emailPwd, emailServer, subject, content, string content", "email setting parameter error");
            }
            MailMessage message = new MailMessage();
            //Set the sender. The sender must be consistent with the mailbox of the set mail sending server
            message.From = new MailAddress(fromMailAddress);
            //You can add multiple recipients in the same way as below
            message.To.Add(toMailAddress);
            //Set email subject
            message.Subject = subject;
            //Set email content
            message.Body = content;

            //set cc
            if (cc != null && !string.IsNullOrWhiteSpace(cc))
            {
                message.CC.Add(cc);
            }
            if (attachFilePath != null && !string.IsNullOrWhiteSpace(attachFilePath))
            {
                attachFilePath = attachFilePath.Replace('\\', Path.DirectorySeparatorChar);
                if (!string.IsNullOrWhiteSpace(attachFilePath) && File.Exists(attachFilePath))
                {
                    //Construct an attachment object
                    Attachment attach = new Attachment(attachFilePath);
                    ////Get file information
                    //ContentDisposition disposition = attach.ContentDisposition;
                    //disposition.CreationDate = File.GetCreationTime(attachFilePath);
                    //disposition.ModificationDate = File.GetLastWriteTime(attachFilePath);
                    //disposition.ReadDate = File.GetLastAccessTime(attachFilePath);
                    //Add attachments to email
                    message.Attachments.Add(attach);
                }
            }

            //Set the mail sending server. The server is different according to the mailbox you use. You can view it in the corresponding mailbox management background
            SmtpClient client = new SmtpClient(emailServer, emailPort);
            //Set the sender's email account and password
            client.Credentials = new NetworkCredential(emailUserNo, emailPwd);
            //set SSL for secure sending
            client.EnableSsl = isSsl;
            //send email
            client.Send(message);
        }


        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="fromMailAddress">发送人邮箱地址</param>
        /// <param name="toMailAddress">接收人邮箱地址</param>
        /// <param name="emailUserNo">发送人邮箱账号</param>
        /// <param name="emailPwd">发送人邮箱密码</param>
        /// <param name="emailServer">发送人邮箱服务器</param>
        /// <param name="emailPort">发送人邮箱服务端口</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="content">邮件内容</param>
        /// <param name="isSsl">是否采用安全链接发送</param>
        /// <param name="attachFilePath">邮件附件</param>
        public static void SendEmail(string fromMailAddress, string toMailAddress, string emailUserNo, string emailPwd, string emailServer, int emailPort, string subject, string content, bool isSsl, string attachFilePath)
        {
            SendEmail(fromMailAddress, toMailAddress, emailUserNo, emailPwd, emailServer, emailPort, subject, content, isSsl, attachFilePath, "");

        }
    }
}